﻿package stfei.HorizList.core
{
    /**
    * AS3版
    * 名称：列表类抽象模板AS3
    * 功能描述：用于将数据按排列显示在场景的影片中，
    * 该类仅是抽象模板，必须靠其它类补助实现功能。
    * 设计：stfei
    * 网站：www.3dflynet.com
    * Emanil: stfei@hotmail.com
    * 更新日期:2009.2.17.11
    */
    import flash.display.*;
    import flash.events.*;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.text.TextField;
    import flash.utils.Timer;
    import flash.xml.XMLDocument;
    public class HorizList extends Sprite
    {
        //接口数据
        private var regPointObj:IRegPoint;
        private var parseXmlObj:IparseXml;
        //影片接口
        protected var _listMc:MovieClip;
        protected var _navBar:MovieClip;
        private var prevBtn:SimpleButton;
        private var nextBtn:SimpleButton;
        private var navText:TextField;
        //类变量
        private var _speed:Number;
        private var _xmlFilePath:String;
        private var dataLong:Number;
        private var page:Number;
        private var pages:Number;
        protected var DataArr:Array;
        protected var RegPointArr:Array;
        protected var _listColumn:Number;
        protected var _listRow:Number;
        protected var _MCW:Number;
        protected var _MCH:Number;
        //------------类属性---------------
        ///设置加载XML文件的路径
        public function set xmlFilePath(val:String):void
        {
            _xmlFilePath = val;
        }
        ///设置列表影片容器
        public function set listMc(val:MovieClip):void
        {
            _listMc = val;
        }
        ///设置导航控件容器
        public function set navBar(val:MovieClip):void
        {
            _navBar = val;
        }
        ///设置延时速度
        public function set speed(val:Number):void
        {
            _speed = val;
        }
        ///设置列表横列数
        public function set listColumn(val:Number):void
        {
            _listColumn = val
        }
        ///设置列表竖行数
        public function set listRow(val:Number):void
        {
            _listRow = val
        }
        ///设置当前页数
        public function set iPage(val:Number):void
        {
            page = val
        }
        //------------构造函数---------------
        public function HorizList(mcW:Number , mcH:Number )
        {
            _MCW = mcW;
            _MCH = mcH;
            _listColumn = 3;
            _listRow = 2;
            page = 1;
            _speed = 10;
        }
        public function open():void
        {
            prevBtn = _navBar.prevBtn;
            nextBtn = _navBar.nextBtn;
            navText = _navBar.navText;
            regPointObj = getRegPoint();
            parseXmlObj = getParseXml();
            RegPointArr = regPointObj.CreatePointVal();
            this.createList()
        }
        //------------类方法---------------
        public function createList():void
        {
            var loader:URLLoader = new URLLoader();
            try
            {
                loader.load(new URLRequest(_xmlFilePath + page));
                loader.addEventListener(Event.COMPLETE , completeHandler);
            }catch (error:Error)
            {
                trace("加载XML错误");
            }
        }
        //完成XMl的加载事件
        private function completeHandler(eve:Event):void
        {
            var PXML:XMLDocument = new XMLDocument();
            PXML.ignoreWhite = true;
            PXML.parseXML(eve.currentTarget.data);
            dataLong = PXML.firstChild.attributes.dataLong;
            pages = Math.ceil(dataLong / (_listColumn * _listRow));
            DataArr = parseXmlObj.getArray(PXML.firstChild);
            //创建导航条
            prevBtn.addEventListener(MouseEvent.CLICK , prevClick);
            nextBtn.addEventListener(MouseEvent.CLICK , nextClick);
            navText.text = (page) + "/" + pages + "页 共 " + dataLong + "条";
            if (_listMc.numChildren != 0)
            {
                this.DelconMc();
            }
            //计数
            var control:Timer = new Timer(_speed , RegPointArr.length);
            control.addEventListener("timer" , looplist);
            control.start();
        }
        //条件循环事件
        private function looplist(eve:TimerEvent):void
        {
            var TCount:Number = eve.currentTarget.currentCount;
            if (TCount <= DataArr.length)
            {
                this.crateMovieClip(TCount);
            }
        }
        ///创建影片抽象方法
        protected function crateMovieClip(tol:Number):void
        {
            throw new Error("ArgumentError");
        }
        ///xml解析抽象接口
        protected function getParseXml():IparseXml
        {
            throw new Error("ArgumentError");
        }
        ///注册点抽象接口
        protected function getRegPoint():IRegPoint
        {
            throw new Error("ArgumentError");
        }
        //下一页点击事件
        private function nextClick(e:MouseEvent):void
        {
            if ((page + 1) <= pages)
            {
                page++;
                createList();
            }
        }
        //上一页点击事件
        private function prevClick(e:MouseEvent):void
        {
            if ((page - 1) > 0)
            {
                page--;
                createList();
            }
        }
        //清除影片容器子对象
        private function DelconMc():void
        {
            for (var i:int = _listMc.numChildren - 1; i >= 0; i--)
            {
                _listMc.removeChildAt(0);
            }
        }
    }
}
